/* configFile.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fileHandler.h"
#include "configFile.h"

/* Structs */
typedef struct {
	int httpPort;
	int telnetPort;
	char wwwPath[80];
	char password[80];
	char logFilePath[80];
} configT;
/* Global variable*/
static configT configValues;

/* Functions */
/* Function: readConfigFile
* Usage: result = readConfigFile();
* -------------------------------- 
* This function opens the config-file, reads some values and save them in a struct. If it sucessfully opened a file and read
* the values it will return that the result was ok, else it will return that the file was not found.
*/ 
int readConfigFile(void) {
	char *fileName = "configFile.txt";
	FILE *outfile;
	outfile = fopen(fileName, "r");

	if (outfile == NULL)
		return FILE_NOT_FOUND;

	fscanf(outfile, "%*s %d", &configValues.httpPort);
	fscanf(outfile, "%*s %d", &configValues.telnetPort);
	fscanf(outfile, "%*s %s", configValues.wwwPath);
	fscanf(outfile, "%*s %s", configValues.password);
	fscanf(outfile, "%*s %s", configValues.logFilePath);
	fclose(outfile);

	printf("\nHTTP: %d\nTelnet: %d\nRoot: %s\nPassword: %s\nLogfile location: %s\n\n", configValues.httpPort, configValues.telnetPort, configValues.wwwPath, configValues.password, configValues.logFilePath);

	return RESULT_OK;
}
/* Function: getHttpPort
* Usage: port = getHttpPort();
* -------------------------------- 
* This function returns the value of the http port.
*/ 
int getHttpPort(void){
	return configValues.httpPort;
}
/* Function: geTelnetPort
* Usage: port = getTelnetPort();
* -------------------------------- 
* This function returns the value of the telnet port.
*/ 
int getTelnetPort(void){
	return configValues.telnetPort;
}
/* Function: getFilesPath
* Usage: path = getFilesPath();
* -------------------------------- 
* This function returns the wwwPath.
*/ 
char* getWwwPath(void){
	return configValues.wwwPath;
}
/* Function: getPassword
* Usage: password = getPassword(); 
* -------------------------------- 
* This function returns the the password.
*/ 
char* getPassword(void){
	return configValues.password;

}
/* Function: getLoggPath
* Usage: path = getLoggPath(); 
* -------------------------------- 
* This function returns the logg path.
*/ 
char* getLogFilePath(void){
	return configValues.logFilePath;
}